package com.adaxi.adc.common.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;


/**
 * 通用的Reids序列化方式配置
 * 注意：每个子模块必须引用配置加入到Spring容器中
 */
public class CommonRedisConfig {

    public static   RedisTemplate<String, Serializable>  redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        // 设置key序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // string数据类型的key序列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // hash数据类型的key序列化
        // 设置value序列化方式
        redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer()); // string数据类型的value序列化
        redisTemplate.setHashValueSerializer(new GenericFastJsonRedisSerializer()); // hash数据类型的value序列化
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return  redisTemplate;
    }
}
